package com.cdn.allmobilestore.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

import com.cdn.allmobilestore.R;
import com.cdn.allmobilestore.activity.BaseActivity;
import com.cdn.allmobilestore.model.Account;
import com.cdn.allmobilestore.model.Campaign;
import com.cdn.allmobilestore.model.Campaign.CampaignType;
import com.cdn.allmobilestore.ulti.GlobalApplication;
import com.cdn.allmobilestore.ulti.Log;
import com.cdn.allmobilestore.ulti.LoginManager;
import com.cdn.allmobilestore.ulti.LoginManager.LoginCallback;

public class AdvFragment extends BaseFragment 
{
	
	WebView webView;
	TextView btnCancel;
	TextView btnLogin;
	boolean isLoad;
	String url;
	
	LoginCallback loginCallBack = new LoginCallback() {
		
		@Override
		public void onLoginSucceed(Account mAccount) {
			((BaseActivity)AdvFragment.this.getActivity()).setDisplayPopUp(false);
			Campaign mCampaign = GlobalApplication._().getmCampaign();
			Intent i = null;
			
			if(mCampaign.getmType() == CampaignType.CAMPAIGN){
				String url =  mCampaign.getmCampaignUrl();
				i = new Intent(Intent.ACTION_VIEW);
				i.setData(Uri.parse(url));
				
			}
			else if(mCampaign.getmType() == CampaignType.APPDETAIL){
				
				i = new Intent(Intent.ACTION_VIEW, Uri.parse("market://detail?id="+ mCampaign.getmCampaignUrl()));
			}
			startActivity(i);
		}
		
		@Override
		public void onLoginFailed() {
			// TODO Auto-generated method stub
			
		}
	};
	
	public AdvFragment() {
		super();  
		// TODO Auto-generated constructor stub
	}
	
	public AdvFragment(boolean load,String url) {
		super();
		isLoad = load;
		this.url = url;
		Log.v("is Load : " + isLoad);
		Log.v("this.url:" + this.url);
	}
	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		//
		
	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, final ViewGroup container,
			Bundle savedInstanceState) {
	
		View view = inflater.inflate(R.layout.fragment_advertise, container, false);
		this.webView = (WebView)view.findViewById(R.id.webview);

		this.btnCancel =(TextView)view.findViewById(R.id.btn_skip);
		this.btnLogin =(TextView)view.findViewById(R.id.btn_login);
		
		this.btnLogin.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Campaign mCampaign = GlobalApplication._().getmCampaign();
				if(mCampaign.isRequireLogin())
				 LoginManager.getManager().login(loginCallBack);
				else
				 loginCallBack.onLoginSucceed(null);
			}
		});
		
		this.btnCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AdvFragment.this.webView.stopLoading();
				((BaseActivity) AdvFragment.this.getActivity()).setDisplayPopUp(false);				
			}
		});
		this.webView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize,long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
				quotaUpdater.updateQuota(estimatedSize * 2); // try to keep quota size as big as possible else database will not get created in HTML 5 app
			}
		});
		this.webView.setWebViewClient(new WebViewClient() {
	        @Override
	        public boolean shouldOverrideUrlLoading(WebView view, String url) {
	        	
	        	Uri uri = Uri.parse(url);
		        String scheme 	= uri.getScheme();
		        String host 	= uri.getHost();
		        Log.v("host:"+ host);
		        if(host.equals("m.apps.allmobile.vn"))
		        {
		        	if(uri.getQueryParameter("ok") != null)
			        {
			        	int ok = Integer.parseInt(uri.getQueryParameter("ok")) ;
				        if(ok == 1)
				        {
				        	view.stopLoading();				        	
				        	((BaseActivity)AdvFragment.this.getActivity()).setDisplayPopUp(false);				        	
				        }
			        }
		        	else
		        	{		        	
		        		view.loadUrl(url);		        	
		        	}
		        }

	            return true;
	        }
	    });

			WebSettings settings = this.webView.getSettings();
			settings.setJavaScriptEnabled(true);
			settings.setJavaScriptCanOpenWindowsAutomatically(true);
			settings.setDatabaseEnabled(true);
			settings.setUseWideViewPort(true);
			settings.setLoadWithOverviewMode(true);
			if(isLoad)
			{
				Log.v("this.url" + this.url);
				this.webView.loadUrl(this.url);
			}
				
		return view;
	}
	
	
	public void loadUrl(String url)
	{
		Log.v("load Url");
		this.webView.loadUrl(url);
	}

	
	
	
}
